package com.sxbbc.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.MerchantMapping;
import com.sxbbc.api.service.IBusinessService;
import com.sxbbc.api.service.IOrderService;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.dto.jz.OrderBaseVo;
import com.sxbbc.common.core.dto.jz.OrderDetailVo;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.OrderTypeEnums;
import com.sxbbc.common.core.service.IChickenSubscribeService;
import com.sxbbc.common.core.service.IIndustryService;
import com.sxbbc.common.core.service.IOrderMainService;
import com.sxbbc.common.core.service.ISysCashDetailsService;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  商家中心板块
 * </p>
 *
 * @author zhangq
 * @since 2020/4/18
 */
@RestController

public class MerchantController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderMainService orderMainService;
    @Autowired
    private IChickenSubscribeService iChickenSubscribeService;
    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private IIndustryService iIndustryService;
    @Autowired
    private IBusinessService iBusinessService;
    @Autowired
    private ISysCashDetailsService iSysCashDetailsService;

    //region 基础功能

    /**
     * 商家中心-申请入驻所属行业
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANT_SELECT_INDUSTRY)
    public RestResponse selectIndustry(BasePage<Industry> page, Industry industry,String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        page.setPaging(false);
        Page<Industry> industryPage = iIndustryService.queryIndustryAndDetail(page, industry);
        return GetRest.getSuccess("", industryPage);
    }

    /**
     * 商家中心-获取申请入驻让利比例
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANT_SELECT_SCAL)
    public RestResponse selectIndustry( String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return GetRest.getSuccess("", Arrays.asList(0.1, 0.2, 0.3, 0.4));
    }


    /**
     * 个人中心-商家中心-首页数据
     */
    @RequestMapping(value = MerchantMapping.MERCHANT_SELECT_INDEXDATA)
    public RestResponse selectMerchantCenter(String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return  iBusinessService.selectMerchantCenterData(cid);
    }

    /**
     * 个人中心-商家中心-修改营业状态 /时间
     */
    @RequestMapping(value = MerchantMapping.ERCHANT_EDIT_OPERATING_STATUS)
    public RestResponse editOperatingStatus(String token, Integer status,String workTime) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.editOperatingStatus(cid, status,workTime);
    }


    /**
     * 个人中心-商家中心-营业额明细列表
     */
    @RequestMapping(value = MerchantMapping.QUERY_TURNOVER_DETAILS_LIST)
    public RestResponse queryTurnoverDetailsList(Integer dateType,String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.queryTurnoverDetailsList(cid, dateType);
    }

    /**
     * 个人中心-商家中心-店铺钱包明细列表
     * @return
     */
    @RequestMapping(value = MerchantMapping.QUERY_WALLET_DETAILS_LIST)
    public RestResponse queryWalletDetailsList(String token,BasePage<BMoneyDetail> page) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.queryWalletDetailsList(cid,page);
    }

    /**
     * 个人中心-商家中心-商家提现页面数据
     */
    @RequestMapping(value = MerchantMapping.MERCHANT_CASH_WITHDRAWAL_PAGE)
    public RestResponse merchantCashWithdrawalPage(String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.merchantCashWithdrawalPage(cid);
    }

    /**
     * 个人中心-商家中心-商家申请提现
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_APPLY_FOR_WITHDRAWAL)
    public RestResponse merchantsCashApply(String token, String payWord,SysCashDetails cash) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        cash.setTargetId(cid);
        return iBusinessService.cashApply(cash,payWord);
    }


    //endregion

    //region 订单核销

    /**
     * 商家中心-线下订单核销信息展示
     * @param token
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_OFFLINEORDER_VERIFYINFO)
    public RestResponse offlineOrderInfo(String token,String verifyCode){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.offlineOrderInfo(cid,verifyCode);
    }

    /**
     * 商家中心-线下订单核销
     * @param verifyCode
     * @param token
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_OFFLINEORDER_VERIFY)
    public RestResponse orderVerify(String verifyCode,String token){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iBusinessService.orderVerify(cid,verifyCode);
    }

    /**
     * 商家中心-预约提鸡订单核销信息展示
     * @param token
     * @param orderCode
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_SUBSCRIBEORDER_VERIFYINFO)
    public RestResponse subscribeOrderVerifyInfo(String token,String orderCode){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iChickenSubscribeService.subscribeOrderVerifyInfo(cid,orderCode);
    }

    /**
     * 商家中心-预约提鸡订单核销
     * @param orderCode
     * @param token
     * @return
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_SUBSCRIBEORDER_VERIFY)
    public RestResponse subscribeOrderVerify(String orderCode,String token){
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        return iChickenSubscribeService.subscribeOrderVerify(cid,orderCode);
    }


    //endregion

    //region订单查询



    /**
     *  商家中心-订单查询-线下订单列表
     *
     * @param orderMain 订单查询数据
     * @param token     获取cid
     * @return 订单列表数据
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_OFFLINEORDER_LIST)
    public RestResponse selectofflineOrderList(BasePage<OrderMain> page, OrderMain orderMain, String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }

        BBusiness business = iBusinessService.getOne(new QueryWrapper<BBusiness>().eq("cid", cid));
        orderMain.setOrderType(OrderTypeEnums.OFFLINE.getId());
        orderMain.setBid(business.getId());
        IPage<OrderBaseVo> pageInfo = orderService.queryOrder(page, orderMain);
        return GetRest.getSuccess("", pageInfo);
    }

    /**
     * 商家中心-订单查询-线下订单详情
     *
     * @param token     获取cid
     * @param orderCode 订单编号
     * @return 订单详情数据
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_OFFLINEORDER_DETAIL)
    public RestResponse getOrderDetail(String token, String orderCode) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        BBusiness business = iBusinessService.getOne(new QueryWrapper<BBusiness>().eq("cid", cid));
        OrderMain order = orderMainService.getByOrderCode(orderCode);
        if (!CalcuUtils.getInstance().isEquals(order.getBid(), business.getId())) {
            return GetRest.getFail("订单数据有误");
        }
        OrderDetailVo result = orderService.handleQueryOrderDetailById(order.getId());
        return GetRest.getSuccess("", result);
    }

    /**
     *  商家中心-订单查询-预约提鸡列表
     *
     * @param chickenSubscribe 订单查询数据
     * @param token     获取cid
     * @return 订单列表数据
     */
    @RequestMapping(value = MerchantMapping.MERCHANTS_QUERY_SUBSCRIBEORDER_LIST)
    public RestResponse selectSubscribeOrderList(BasePage<ChickenSubscribe> page, ChickenSubscribe chickenSubscribe, String token) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        BBusiness business = iBusinessService.getOne(new QueryWrapper<BBusiness>().eq("cid", cid));
        chickenSubscribe.setBid(business.getId());
        IPage<ChickenSubscribe> pageInfo = iChickenSubscribeService.selectChickenSubscribeList(page, chickenSubscribe);
        return GetRest.getSuccess("", pageInfo);
    }

    //endregion
}
